package pers.qianyu.module.core.domain.logging.converter;

import com.alibaba.fastjson.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.*;
import org.springframework.stereotype.*;
import pers.qianyu.module.core.comm.*;
import pers.qianyu.module.core.domain.logging.dto.*;
import pers.qianyu.module.core.domain.logging.entity.*;
import pers.qianyu.module.core.domain.logging.query.*;
import pers.qianyu.module.core.domain.logging.vo.*;
import pers.qianyu.module.core.util.*;
import pers.qianyu.module.logging.enums.*;

import java.util.*;
import java.util.stream.*;

/**
 * @author mizzle rain
 * @date 2020年12月6日22:54:29
 */
@Component
public class RequestLogConverter extends BaseConverter {
    /**
     * query 转换为 po
     *
     * @param requestLogQuery query 对象
     * @return po 对象
     */
    public RequestLogPO convertToPO(RequestLogQuery requestLogQuery) {
        RequestLogPO requestLogPO = new RequestLogPO();
        requestLogPO.setIp(requestLogQuery.getIp());
        requestLogPO.setUrl(requestLogQuery.getUrl());
        requestLogPO.setUserAgent(requestLogQuery.getUserAgent());
        requestLogPO.setRequestMethod(requestLogQuery.getRequestMethod());
        requestLogPO.setClassMethod(requestLogQuery.getClassMethod());
        return requestLogPO;
    }


    public RequestLogPO convertToPO(RequestLogDTO requestLogDTO) {
        RequestLogPO requestLogPO = new RequestLogPO();
        requestLogPO.setIp(requestLogDTO.getIp());
        // ip to address
        requestLogPO.setAddress(IpUtil.getInfoFromIp(requestLogDTO.getIp()));
        requestLogPO.setUrl(requestLogDTO.getUrl());
        requestLogPO.setClassMethod(requestLogDTO.getClassMethod());
        requestLogPO.setResult(requestLogDTO.getResult());
        requestLogPO.setException(requestLogDTO.getException());
        requestLogPO.setRequestMethod(requestLogDTO.getRequestMethod());
        requestLogPO.setRequestParameters(requestLogDTO.getRequestParameters());
        requestLogPO.setUserAgent(requestLogDTO.getUserAgent());
        requestLogPO.setCreateTime(System.currentTimeMillis());
        requestLogPO.setBusinessType(requestLogDTO.getBusinessType().ordinal());
        return requestLogPO;
    }

    public RequestLogVO convertToVO(RequestLogPO requestLogPO) {
        RequestLogVO requestLogVO = new RequestLogVO();
        entityToVO(requestLogPO, requestLogVO);
        requestLogVO.setIp(requestLogPO.getIp());
        requestLogVO.setAddress(requestLogPO.getAddress());
        requestLogVO.setUrl(requestLogPO.getUrl());
        requestLogVO.setClassMethod(requestLogPO.getClassMethod());
        requestLogVO.setResult(requestLogPO.getResult());
        requestLogVO.setException(requestLogPO.getException());
        requestLogVO.setRequestMethod(requestLogPO.getRequestMethod());
        requestLogVO.setUserAgent(requestLogPO.getUserAgent());
        requestLogVO.setRequestParameters(requestLogPO.getRequestParameters());
        requestLogVO.setBusinessType(BusinessType.values()[requestLogPO.getBusinessType()].name());
        // parse user-agent
        UserAgentVO userAgentVO = BrowserUtil.parseUserAgent(requestLogPO.getUserAgent());
        requestLogVO.setBrowserInfo(userAgentVO);
        return requestLogVO;
    }

    public Pagination<RequestLogVO> convertToPagination(Page<RequestLogPO> page) {
        Pagination<RequestLogVO> pagination = new Pagination<>();
        pagination.setTotal(page.getTotal());
        pagination.setSize(page.getSize());
        pagination.setCurrent(page.getCurrent());
        pagination.setRecords(Optional.ofNullable(page.getRecords())
                .map(List::stream)
                .orElseGet(Stream::empty)
                .map(this::convertToVO)
                .collect(Collectors.toList()));
        return pagination;
    }

    public List<RequestLogExportDTO> convertToRequestLogExportDTOList(List<RequestLogPO> requestLogPOS) {
        return Optional.ofNullable(requestLogPOS)
                .map(List::stream)
                .orElseGet(Stream::empty)
                .map(this::convertToRequestLogExportDTO)
                .collect(Collectors.toList());
    }

    /**
     * 将数据库 PO 对象转换为 Excel 导出对象
     *
     * @param requestLogPO PO 对象
     * @return Excel 导出对象
     */
    private RequestLogExportDTO convertToRequestLogExportDTO(RequestLogPO requestLogPO) {
        RequestLogExportDTO requestLogExportDTO = new RequestLogExportDTO();
        requestLogExportDTO.setIp(requestLogPO.getIp());
        requestLogExportDTO.setUrl(requestLogPO.getUrl());
        requestLogExportDTO.setUserAgent(requestLogPO.getUserAgent());
        requestLogExportDTO.setRequestMethod(requestLogPO.getRequestMethod());
        requestLogExportDTO.setClassMethod(requestLogPO.getClassMethod());
        requestLogExportDTO.setRequestParameters(requestLogPO.getRequestParameters());
        requestLogExportDTO.setResult(requestLogPO.getResult());
        requestLogExportDTO.setException(requestLogPO.getException());
        requestLogExportDTO.setCreateTime(DateTimeUtil.toLocalDatetime(requestLogPO.getCreateTime()));
        requestLogExportDTO.setAddress(requestLogPO.getAddress());
        requestLogExportDTO.setBrowserInfo(JSON.toJSONString(BrowserUtil.parseUserAgent(requestLogPO.getUserAgent())));
        requestLogExportDTO.setBusinessType(BusinessType.values()[requestLogPO.getBusinessType()].name());
        return requestLogExportDTO;
    }
}
